<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>numpy.meshgrid中的笛卡尔坐标和矩阵坐标</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>numpy.meshgrid中的笛卡尔坐标和矩阵坐标</h1>    <p>
        under
            <a href="../../tags/numpy/">numpy</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-11-20</p>


    <p><tt class="docutils literal">numpy.meshgrid</tt>可以从坐标轴的坐标生成规则格点，它为前二维网格提供了两种索引方式，笛卡尔坐标（<tt class="docutils literal">indexing=xy</tt>）和矩阵坐标（<tt class="docutils literal">indexing=ij</tt>），如何理解这两种索引方式呢？</p>
<p>按照约定俗成的方式，第i行j列的元素用aij来表示（本文中所有坐标从1开始计数，参数和维度都从0开始计数）：</p>
<pre class="literal-block">
a11 a12 ... a1n
a21 a22 ... a2n
...
am1 am2 ... amn
</pre>
<p>同样按照习惯表示笛卡尔坐标系中的坐标：</p>
<pre class="literal-block">
(x1, yn) (x2, yn) ... (xm, yn)
...
(x1, y2) (x2, y2) ... (xm, y2)
(x1, y1) (x2, y1) ... (xm, y1)
</pre>
<p>按照numpy的约定，二维数组中列是第0维，行是第1维，则上面画出的矩阵坐标中，i坐标轴和j坐标轴分别位于第0维和第1维，笛卡尔坐标中x坐标轴和y坐标轴分别位于第1维和第0维。</p>
<p>在使用<tt class="docutils literal">numpy.meshgrid</tt>的时候，是这样写的：</p>
<div class="highlight"><pre><span></span><span class="n">ii</span><span class="p">,</span> <span class="n">jj</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">indexing</span><span class="o">=</span><span class="s2">&quot;ij&quot;</span><span class="p">)</span>
<span class="n">xx</span><span class="p">,</span> <span class="n">yy</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">indexing</span><span class="o">=</span><span class="s2">&quot;xy&quot;</span><span class="p">)</span>
</pre></div>
<p>根据上面的推论，<tt class="docutils literal">ii</tt>和<tt class="docutils literal">jj</tt>的第0维和第1维分别对应于<tt class="docutils literal">i</tt>和<tt class="docutils literal">j</tt>所代表的坐标系，<tt class="docutils literal">xx</tt>和<tt class="docutils literal">yy</tt>的第0维和第1维分别对应于<tt class="docutils literal">y</tt>和<tt class="docutils literal">x</tt>所代表的坐标系，即以下对应关系成立：</p>
<table border="1" class="docutils">
<colgroup>
<col width="22%" />
<col width="39%" />
<col width="39%" />
</colgroup>
<tbody valign="top">
<tr><td>坐标系</td>
<td>结果的维度0</td>
<td>结果的维度1</td>
</tr>
<tr><td>矩阵坐标系</td>
<td>第0个参数对应的坐标系</td>
<td>第1个参数对应的坐标系</td>
</tr>
<tr><td>笛卡尔坐标系</td>
<td>第1个参数对应的坐标系</td>
<td>第0个参数对应的坐标系</td>
</tr>
</tbody>
</table>
<p>在使用的时候，应该根据数据数组的维度来决定使用哪一种索引方式，举个例子，如果数据是<tt class="docutils literal"><span class="pre">data[lon][lat]</span></tt>，则写成：</p>
<div class="highlight"><pre><span></span><span class="n">lon2d</span><span class="p">,</span> <span class="n">lat2d</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">(</span><span class="n">lon</span><span class="p">,</span> <span class="n">lat</span><span class="p">,</span> <span class="n">indexing</span><span class="o">=</span><span class="s2">&quot;ij&quot;</span><span class="p">)</span>
</pre></div>
<p>如果数据是<tt class="docutils literal"><span class="pre">data[lat][lon]</span></tt>，则可省略<tt class="docutils literal">indexing</tt>参数（默认为<tt class="docutils literal">xy</tt>）：</p>
<div class="highlight"><pre><span></span><span class="n">lon2d</span><span class="p">,</span> <span class="n">lat2d</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">(</span><span class="n">lon</span><span class="p">,</span> <span class="n">lat</span><span class="p">)</span>
</pre></div>
<p>在多维情况下，无论采用哪种索引方式，第<tt class="docutils literal">k</tt>维对应<tt class="docutils literal">numpy.meshgrid</tt>的参数<tt class="docutils literal">k</tt>（<tt class="docutils literal">k &gt; 1</tt>）。</p>
<p>（完）</p>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>